//
//  YbCell.swift
//  Auscultation
//
//  Created by 来 on 2020/12/8.
//

import UIKit

class YbCell: UICollectionViewCell {
    
    var tzzx: UILabel!
    var cjsj: UILabel!
    var status: UILabel!
    var bzb: UILabel!
    var shb: UILabel!
    
    var deleted: UILabel!
    
    var status_v: Int!
    
    // 流程
    var bzks_i: UIImageView!
    var bzks_l: UILabel!
    var bzks: UILabel!
    var bzwc_i: UIImageView!
    var bzwc_l: UILabel!
    var bzwc: UILabel!
    var shks_i: UIImageView!
    var shks_l: UILabel!
    var shks: UILabel!
    var shwc_i: UIImageView!
    var shwc_l: UILabel!
    var shwc: UILabel!
    var lc_line1: UIView!
    var lc_line2: UIView!
    var lc_line3: UIView!
    
    var url: String!
    var allTime: String = "00:00"
    
    // 播放
    var play_button: UIButton!
    // 时间
    var play_time: UILabel!
    // 进度
    var play_line: UIProgressView!
    
    var player: LGFAudioPlayer!
    
    deinit {
        debugPrint("LGFAudioPlayer 已释放")
        player?.stop()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        let back_view = UIView.init()
        back_view.backgroundColor = UIColor.white
        back_view.clipsToBounds = true
        back_view.layer.cornerRadius = 4
        addSubview(back_view)
        back_view.snp.makeConstraints { (make) in
            make.top.equalTo(8)
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.bottom.equalTo(-8)
        }
        
        tzzx = UILabel.init()
        tzzx.text = ""
        tzzx.lgf_AddShowAllText()
        tzzx.font = UIFont.systemFont(ofSize: 16)
        tzzx.textColor = UIColor.init(lgf_HexString: "222222")
        back_view.addSubview(tzzx)
        tzzx.snp.makeConstraints { (make) in
            make.top.equalTo(15)
            make.left.equalTo(15)
            make.width.lessThanOrEqualToSuperview().multipliedBy(0.7).offset(-40)
        }
        
        deleted = UILabel.init()
        deleted.text = ""
        deleted.clipsToBounds = true
        deleted.textAlignment = .center
        deleted.font = UIFont.systemFont(ofSize: 11)
        deleted.backgroundColor = UIColor.init(lgf_HexString: "0F88F2")?.withAlphaComponent(0.1)
        deleted.textColor = UIColor.init(lgf_HexString: "0F88F2")
        deleted.layer.borderColor = UIColor.init(lgf_HexString: "0F88F2")?.withAlphaComponent(0.2).cgColor
        deleted.layer.borderWidth = 1
        deleted.layer.cornerRadius = 4
        back_view.addSubview(deleted)
        deleted.snp.makeConstraints { (make) in
            make.centerY.equalTo(tzzx)
            make.left.equalTo(tzzx.snp.right).offset(5)
            make.width.equalTo(36)
            make.height.equalTo(19)
        }
        
        status = UILabel.init()
        status.text = ""
        status.lgf_AddShowAllText()
        status.font = UIFont.systemFont(ofSize: 12)
        status.textColor = UIColor.init(lgf_HexString: "0F88F2")
        back_view.addSubview(status)
        status.snp.makeConstraints { (make) in
            make.centerY.equalTo(tzzx)
            make.right.equalTo(-15)
        }
        
        cjsj = UILabel.init()
        cjsj.text = "创建时间："
        cjsj.lgf_AddShowAllText()
        cjsj.font = UIFont.systemFont(ofSize: 12)
        cjsj.textColor = UIColor.init(lgf_HexString: "8D969D")
        back_view.addSubview(cjsj)
        cjsj.snp.makeConstraints { (make) in
            make.top.equalTo(tzzx.snp.bottom).offset(3)
            make.left.equalTo(tzzx)
        }
        
        let view1 = UIView.init()
        view1.backgroundColor = UIColor.init(lgf_HexString: "F5FAFF")
        back_view.addSubview(view1)
        view1.snp.makeConstraints { (make) in
            make.top.equalTo(cjsj.snp.bottom).offset(12)
            make.centerX.equalToSuperview()
            make.width.equalToSuperview().offset(-30)
            make.height.equalTo(100)
        }
        
        bzb = UILabel.init()
        bzb.text = "标注包：0"
        bzb.lgf_AddShowAllText()
        bzb.font = UIFont.systemFont(ofSize: 12)
        bzb.textColor = UIColor.init(lgf_HexString: "8D969D")
        view1.addSubview(bzb)
        bzb.snp.makeConstraints { (make) in
            make.top.equalTo(13)
            make.centerX.equalToSuperview().multipliedBy(0.5)
            make.width.equalTo(100)
        }
        
        let line1 = UIView.init()
        line1.backgroundColor = UIColor.init(lgf_HexString: "8D969D")
        view1.addSubview(line1)
        line1.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalTo(bzb)
            make.width.equalTo(1)
            make.height.equalTo(8)
        }
        
        shb = UILabel.init()
        shb.text = "审核包：0"
        shb.lgf_AddShowAllText()
        shb.font = UIFont.systemFont(ofSize: 12)
        shb.textColor = UIColor.init(lgf_HexString: "8D969D")
        view1.addSubview(shb)
        shb.snp.makeConstraints { (make) in
            make.top.equalTo(13)
            make.centerX.equalToSuperview().multipliedBy(1.5)
            make.width.equalTo(100)
        }
        
        bzks_l = UILabel.init()
        bzks_l.text = "1"
        bzks_l.lgf_AddShowAllText()
        bzks_l.layer.cornerRadius = 8.5
        bzks_l.clipsToBounds = true
        bzks_l.textAlignment = .center
        bzks_l.font = UIFont.boldSystemFont(ofSize: 12)
        bzks_l.textColor = UIColor.white
        bzks_l.backgroundColor = UIColor.init(lgf_HexString: "D0E3F6")
        view1.addSubview(bzks_l)
        bzks_l.snp.makeConstraints { (make) in
            make.top.equalTo(bzb.snp.bottom).offset(12)
            make.width.height.equalTo(17)
            make.centerX.equalToSuperview().multipliedBy(0.25)
        }
        bzks_i = UIImageView.init(image: #imageLiteral(resourceName: "duihao@1.5x"))
        view1.addSubview(bzks_i)
        bzks_i.snp.makeConstraints { (make) in
            make.top.equalTo(bzb.snp.bottom).offset(12)
            make.width.height.equalTo(17)
            make.centerX.equalToSuperview().multipliedBy(0.25)
        }
        bzks = UILabel.init()
        bzks.text = "标注开始"
        bzks.lgf_AddShowAllText()
        bzks.font = UIFont.systemFont(ofSize: 13)
        bzks.textColor = UIColor.init(lgf_HexString: "0F88F2")
        view1.addSubview(bzks)
        bzks.snp.makeConstraints { (make) in
            make.top.equalTo(bzks_i.snp.bottom).offset(10)
            make.centerX.equalTo(bzks_i)
        }
        
        bzwc_l = UILabel.init()
        bzwc_l.text = "2"
        bzwc_l.lgf_AddShowAllText()
        bzwc_l.layer.cornerRadius = 8.5
        bzwc_l.clipsToBounds = true
        bzwc_l.textAlignment = .center
        bzwc_l.font = UIFont.boldSystemFont(ofSize: 12)
        bzwc_l.textColor = UIColor.white
        bzwc_l.backgroundColor = UIColor.init(lgf_HexString: "D0E3F6")
        view1.addSubview(bzwc_l)
        bzwc_l.snp.makeConstraints { (make) in
            make.top.equalTo(bzb.snp.bottom).offset(12)
            make.width.height.equalTo(17)
            make.centerX.equalToSuperview().multipliedBy(0.75)
        }
        bzwc_i = UIImageView.init(image: #imageLiteral(resourceName: "duihao@1.5x"))
        view1.addSubview(bzwc_i)
        bzwc_i.snp.makeConstraints { (make) in
            make.top.equalTo(bzb.snp.bottom).offset(12)
            make.width.height.equalTo(17)
            make.centerX.equalToSuperview().multipliedBy(0.75)
        }
        bzwc = UILabel.init()
        bzwc.text = "标注完成"
        bzwc.lgf_AddShowAllText()
        bzwc.font = UIFont.systemFont(ofSize: 13)
        bzwc.textColor = UIColor.init(lgf_HexString: "0F88F2")
        view1.addSubview(bzwc)
        bzwc.snp.makeConstraints { (make) in
            make.top.equalTo(bzwc_i.snp.bottom).offset(10)
            make.centerX.equalTo(bzwc_i)
        }
        
        shks_l = UILabel.init()
        shks_l.text = "3"
        shks_l.lgf_AddShowAllText()
        shks_l.layer.cornerRadius = 8.5
        shks_l.clipsToBounds = true
        shks_l.textAlignment = .center
        shks_l.font = UIFont.boldSystemFont(ofSize: 12)
        shks_l.textColor = UIColor.white
        shks_l.backgroundColor = UIColor.init(lgf_HexString: "D0E3F6")
        view1.addSubview(shks_l)
        shks_l.snp.makeConstraints { (make) in
            make.top.equalTo(bzb.snp.bottom).offset(12)
            make.width.height.equalTo(17)
            make.centerX.equalToSuperview().multipliedBy(1.25)
        }
        shks_i = UIImageView.init(image: #imageLiteral(resourceName: "duihao@1.5x"))
        view1.addSubview(shks_i)
        shks_i.snp.makeConstraints { (make) in
            make.top.equalTo(bzb.snp.bottom).offset(12)
            make.width.height.equalTo(17)
            make.centerX.equalToSuperview().multipliedBy(1.25)
        }
        shks = UILabel.init()
        shks.text = "审核开始"
        shks.lgf_AddShowAllText()
        shks.font = UIFont.systemFont(ofSize: 13)
        shks.textColor = UIColor.init(lgf_HexString: "0F88F2")
        view1.addSubview(shks)
        shks.snp.makeConstraints { (make) in
            make.top.equalTo(shks_i.snp.bottom).offset(10)
            make.centerX.equalTo(shks_i)
        }
        
        shwc_l = UILabel.init()
        shwc_l.text = "4"
        shwc_l.lgf_AddShowAllText()
        shwc_l.layer.cornerRadius = 8.5
        shwc_l.clipsToBounds = true
        shwc_l.textAlignment = .center
        shwc_l.font = UIFont.boldSystemFont(ofSize: 12)
        shwc_l.textColor = UIColor.white
        shwc_l.backgroundColor = UIColor.init(lgf_HexString: "D0E3F6")
        view1.addSubview(shwc_l)
        shwc_l.snp.makeConstraints { (make) in
            make.top.equalTo(bzb.snp.bottom).offset(12)
            make.width.height.equalTo(17)
            make.centerX.equalToSuperview().multipliedBy(1.75)
        }
        shwc_i = UIImageView.init(image: #imageLiteral(resourceName: "duihao@1.5x"))
        view1.addSubview(shwc_i)
        shwc_i.snp.makeConstraints { (make) in
            make.top.equalTo(bzb.snp.bottom).offset(12)
            make.width.height.equalTo(17)
            make.centerX.equalToSuperview().multipliedBy(1.75)
        }
        shwc = UILabel.init()
        shwc.text = "审核完成"
        shwc.lgf_AddShowAllText()
        shwc.font = UIFont.systemFont(ofSize: 13)
        shwc.textColor = UIColor.init(lgf_HexString: "0F88F2")
        view1.addSubview(shwc)
        shwc.snp.makeConstraints { (make) in
            make.top.equalTo(shwc_i.snp.bottom).offset(10)
            make.centerX.equalTo(shwc_i)
        }
        
        lc_line1 = UIView.init()
        lc_line1.backgroundColor = UIColor.init(lgf_HexString: "0F88F2")
        view1.addSubview(lc_line1)
        lc_line1.snp.makeConstraints { (make) in
            make.centerY.equalTo(bzks_i)
            make.left.equalTo(bzks_i.snp.right)
            make.right.equalTo(bzwc_i.snp.left)
            make.height.equalTo(2)
        }
        
        lc_line2 = UIView.init()
        lc_line2.backgroundColor = UIColor.init(lgf_HexString: "0F88F2")
        view1.addSubview(lc_line2)
        lc_line2.snp.makeConstraints { (make) in
            make.centerY.equalTo(bzks_i)
            make.left.equalTo(bzwc_i.snp.right)
            make.right.equalTo(shks_i.snp.left)
            make.height.equalTo(2)
        }
        
        lc_line3 = UIView.init()
        lc_line3.backgroundColor = UIColor.init(lgf_HexString: "0F88F2")
        view1.addSubview(lc_line3)
        lc_line3.snp.makeConstraints { (make) in
            make.centerY.equalTo(bzks_i)
            make.left.equalTo(shks_i.snp.right)
            make.right.equalTo(shwc_i.snp.left)
            make.height.equalTo(2)
        }
        
        // 播放
        play_button = UIButton.init(type: .custom)
        play_button.lgf_ViewName = "暂停"
        play_button.setBackgroundImage(#imageLiteral(resourceName: "bofang@1.5x"), for: .normal)
        play_button.addTarget(self, action: #selector(play), for: .touchUpInside)
        back_view.addSubview(play_button)
        play_button.snp.makeConstraints { (make) in
            make.top.equalTo(view1.snp.bottom).offset(12)
            make.left.equalTo(view1)
            make.width.height.equalTo(25)
        }
        
        play_time = UILabel.init()
        play_time.text = "00:00/00:00"
        play_time.font = UIFont.systemFont(ofSize: 12)
        play_time.textColor = UIColor.init(lgf_HexString: "CFD4D9")
        back_view.addSubview(play_time)
        play_time.snp.makeConstraints { (make) in
            make.centerY.equalTo(play_button)
            make.right.equalTo(view1.snp.right)
            make.width.equalTo(80)
        }
        
        play_line = UIProgressView.init()
        play_line.progress = 0.0
        play_line.progressTintColor = UIColor.init(lgf_HexString: "0F88F2")
        play_line.trackTintColor = UIColor.init(lgf_HexString: "CFD4D9")
        back_view.addSubview(play_line)
        play_line.snp.makeConstraints { (make) in
            make.centerY.equalTo(play_button)
            make.left.equalTo(play_button.snp.right).offset(10)
            make.right.equalTo(play_time.snp.left).offset(-10)
            make.height.equalTo(1)
        }
        
    }
    
    func initPlay() {
        if player == nil {
            NotificationCenter.default.addObserver(self, selector: #selector(CellPlayerStop(_:)), name: NSNotification.Name(rawValue: "YbCellPlayerStop"), object: nil)
            player = LGFAudioPlayer()
            player.delegate = self
        }
    }
    
    @objc func play() {
        if status_v == 10 {
            return
        }
        if play_button.lgf_ViewName == "暂停" {
            play_button.lgf_ViewName = "恢复"
            play_button.setBackgroundImage(#imageLiteral(resourceName: "暂停@1.5x"), for: .normal)
        } else {
            play_button.lgf_ViewName = "暂停"
            play_button.setBackgroundImage(#imageLiteral(resourceName: "播放@1.5x"), for: .normal)
        }
        if player.state == .playing {
            player.paused()
        } else if player.state == .paused {
            player.resume()
        } else {
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "YbCellPlayerStop"), object: nil)
            player.playOnLine(EWEUrl.File_URL + url)
        }
    }
    
    @objc func CellPlayerStop(_ sender: Notification) {
        if player.state != .stopped {
            player?.stop()
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension YbCell: LGFAudioPlayerDelegate {
    func LGFAudioPlayerState(_ player: STKAudioPlayer) {
//        allTime = self.player.getAllTime()
    }
    
    func LGFAudioPlayerRun(_ player: STKAudioPlayer) {
        if player.duration > 0 {
            play_line.progress = Float(player.progress / player.duration)
            play_time.text = self.player.getTime() + "/" + allTime
        }
    }
    
    func LGFAudioPlayerStop(_ player: STKAudioPlayer) {
        play_button.lgf_ViewName = "暂停"
        play_button.setBackgroundImage(#imageLiteral(resourceName: "播放@1.5x"), for: .normal)
        play_line.progress = 0.0
        play_time.text = "00:00/" + allTime
    }
}
